package com.freewater.springboot.demo.core.beanwrapper;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.PropertyValue;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        BeanWrapper beanWrapperOfCar = PropertyAccessorFactory.forBeanPropertyAccess(car); // 等同于 new BeanWrapperImpl(car);
        PropertyValue brandValue = new PropertyValue("brand", "东风");
        PropertyValue maxSpeedValue = new PropertyValue("maxSpeed", 250);
        PropertyValue priceValue = new PropertyValue("price", 100 * 10000);
        beanWrapperOfCar.setPropertyValue(brandValue);
        beanWrapperOfCar.setPropertyValue(maxSpeedValue);
        beanWrapperOfCar.setPropertyValue(priceValue);

        Man man = new Man();
        BeanWrapper beanWrapperOfMan = PropertyAccessorFactory.forBeanPropertyAccess(man);
        beanWrapperOfMan.setAutoGrowNestedPaths(true);
        PropertyValue nameValue = new PropertyValue("name", "Jack Ma");
        PropertyValue ageValue = new PropertyValue("age", 23);
        PropertyValue carValue = new PropertyValue("car", car);
        String[] hobbies = {"跑步", "唱歌", "看书"};
        PropertyValue hobbiesValue = new PropertyValue("hobbies", hobbies);
        Map<String, Object> relatives = new HashMap<>();
        relatives.put("father", "Jack Ma");
        relatives.put("son", "Lite Jack Ma");
        PropertyValue relativesValues = new PropertyValue("relatives", relatives);
        beanWrapperOfMan.setPropertyValue(nameValue);
        beanWrapperOfMan.setPropertyValue(ageValue);
        beanWrapperOfMan.setPropertyValue(carValue);
        beanWrapperOfMan.setPropertyValue(hobbiesValue);
        beanWrapperOfMan.setPropertyValue(relativesValues);
        beanWrapperOfMan.setPropertyValue("car.brand", "东方红");

        System.out.println(man);
        System.out.println(beanWrapperOfMan.getWrappedInstance());
        System.out.println(man == beanWrapperOfMan.getWrappedInstance());

        int age = (int) beanWrapperOfMan.getPropertyValue("age");
        String hobby = (String) beanWrapperOfMan.getPropertyValue("hobbies[1]");
        String brand = (String) beanWrapperOfMan.getPropertyValue("car.brand");
        String relative = (String) beanWrapperOfMan.getPropertyValue("relatives['father']");
        String result = String.format("%s is %d years old ,is interested in %s, has a relative named %s , also has a %s car!",
                man.getName(), age, hobby, relative, brand);
        System.out.println(result);

        // orders
        beanWrapperOfMan.setPropertyValue("orders[0].items[0].productName", "iphone");
        System.out.println(man.getOrders().getFirst().getItems().getFirst().getProductName());
        Order order = new Order("SN000000000001");
        beanWrapperOfMan.setPropertyValue("relatives['order']", order);
        System.out.println(((Order)man.getRelatives().get("order")).getSno());
    }
}
